import { cloneDeep } from 'lodash-es'
import { http } from '@/utils/http'
import { ElMessage } from 'element-plus'
import { getPlatformUser, getMoneyLimit } from '@/api/user'
import { checkRoles } from '@/stores/roles'
import _ from 'lodash-es'

export const shortcutSellStore = () => {
    const userStore = useUserStore()
    const state = reactive({
        show: false,
        addshow: false,

        formData: {
            keyPressed: '',
            name: '',
            bc_type: '',
            points: 0,
            userId: '',
            user: '',
            stake: 1000,
            order_mode: '',
            runtime: '',
            money_mode: '',
            user_id_list: [],
            order_second: 5,
            sport: 'soccer'
        },
        shortcutList: [],
        listloading: false,
        updatashortcut: {
            type: 'create',
            id: 1
        },
        users: [],

        userdialog: {
            show: false,
            type: 'radio',
            radiouser: '',
            field: ''
        }
    })

    const openShortcut = () => {
        state.show = true
        state.addshow = false
        getshortcutkeylist(true)
        window.electron.deleteshortcut()
    }

    const openaddShortcut = () => {
        state.addshow = true
        state.updatashortcut = {
            type: 'create',
            id: state.shortcutList.length + 1
        }
        state.formData = {
            keyPressed: '',
            name: '',
            bc_type: '',
            points: 0,
            userId: '',
            user: '',
            stake: 1000,
            order_mode: '',
            runtime: '',
            money_mode: '',
            user_id_list: [],
            order_second: 5,
            sport: 'soccer'
        }
    }

    const editShortcut = (row) => {
        state.addshow = true
        state.updatashortcut = {
            type: 'update',
            id: row.id
        }
        state.formData = JSON.parse(JSON.stringify(row))
        getPlatformUser({ id: state.formData.userId }).then(res => {
            state.users = res.data
        })
    }

    const closeaddShortcut = () => {
        state.addshow = false
    }

    const getshortcutkeylist = (loading) => {
        if (loading) {
            state.listloading = true
        }
        http.get('order/shortcut_key_list').then((res) => {
            state.shortcutList = []
            Object.entries(res.data).forEach(([key, value]) => {
                state.shortcutList.push(value)
            });
            state.listloading = false

        }).catch(() => {
            state.listloading = false
        })
    }

    const deleteShortcut = (row) => {
        http.delete(`/order/del_shortcut_key?id=${row.id}`).then((res) => {
            getshortcutkeylist(true)
        })
    }

    onMounted(() => {
        // getshortcutkeylist(true)
    })

    onUnmounted(() => {

    })

    return {
        state,
        openaddShortcut,
        closeaddShortcut,
        getshortcutkeylist,
        editShortcut,
        deleteShortcut,
        openShortcut
    }
}
